#include"subpl.h"

int status;
int i,j;

int makeSubProblem( int* vertex, int numVertex, double* k){
	env = CPXopenCPLEX(&status);
	lp = CPXcreateprob(env, &status,"Formula f");
	double ub[ numVertex*numVertex + numVertex ];
	double lb[ numVertex*numVertex + numVertex ];
	double obj[ numVertex*numVertex + numVertex ];
	char type[ numVertex*numVertex + numVertex ];
	for(i = 0; i < numVertex; i++){
		for(j = 0; j < numVertex; j++){
			if(i < j)obj[i*numVertex + j] = weigths[vertex[i]][vertex[j]];
			else { 
				if(i == j) obj[i*numVertex + j] = k[vertex[i]];
				else obj[i*numVertex + j] = 100.0;
			}
			lb[i*numVertex +j] = 0.0;
			ub[i*numVertex +j] = 1.0;
			type[i*numVertex +j] = 'B';
		}
	}
	for(j = numVertex*numVertex; j< numVertex*numVertex + numVertex; j++){
		obj[j] = 0.0;
		lb[j] = 0.0;
		ub[j] = 1.0;
		type[j] = 'B';
	}
	status = CPXnewcols (env, lp, numVertex*numVertex + numVertex, obj, lb, ub, type, NULL);
	biuldProblem(numVertex);
	return 0;
}

int biuldProblem(int numVertex){
	long constrains = 0;
	long coef = 0;
	int rows[2*numVertex*numVertex];
	double values[numVertex*numVertex*numVertex];
	int valuesColumns[numVertex*numVertex*numVertex];
	double b[2*numVertex*numVertex];
	char sense[2*numVertex*numVertex];
	/*soma dos z'is*/
	rows[0] = 0;
	constrains++;
	for(j =0; j< numVertex; j++){
		values[coef] = 1.0;
		valuesColumns[coef] = numVertex*numVertex +j;
		coef++;
	}
	/*soma dos zis*/
	rows[constrains] = coef;
	sense[constrains - 1] = 'E';
	b[constrains - 1] = 1.0;
	constrains++;
	/*restriçoes de x e z*/
	for(i = 0; i< numVertex; i++){
		values[coef] = 1.0;
		values[coef+1] = -1.0;
		valuesColumns[coef]= i*numVertex + i;
		valuesColumns[coef+1]= numVertex*numVertex + i;
		coef +=2;
		rows[constrains] = coef;
		sense[constrains - 1] = 'G';
		b[constrains - 1] = 0.0;
		constrains++;
	}
	/*restriçoes de y e x*/
	for(i = 0; i<numVertex; i++){
		for(j = i+1; j < numVertex; j++){
			values[coef] = 1.0;
			values[coef + 1 ] = -1.0;
			valuesColumns[coef]= j*numVertex + j;
			valuesColumns[coef+1]= numVertex*i + j;
			coef += 2;
			rows[constrains] = coef;
			sense[constrains - 1] = 'G';
			b[constrains - 1] = 0.0;
			constrains++;
		}
	}
	/*restrições de x e z*/
	for(i = 0; i< numVertex; i++){
		for(j = i; j < numVertex; j++){
			values[coef] = -1.0;
			valuesColumns[coef]= numVertex*numVertex + j;
			coef++;
		}
		for(j = i+1; j < numVertex; j++){
			values[coef] = 1.0;
			valuesColumns[coef]= i*numVertex + j;
			coef++;
		}
		rows[constrains] = coef;
		sense[constrains - 1] = 'E';
		b[constrains - 1] = 0.0;
		constrains++;
	}
	return CPXaddrows(env, lp, 0, constrains-1, coef, b, sense, rows, valuesColumns, values, NULL, NULL);
}

double getSolution(){
	CPXwriteprob (env, lp, "myteste.lp", NULL);
	CPXmipopt(env, lp);
	double objval = 0.0;
	CPXgetbestobjval(env, lp, &objval);
	return objval;
}

void fValue(int numVertex, long *f){
	double values[numVertex*numVertex + numVertex];
	CPXgetx(env, lp, values, 0, numVertex*numVertex + numVertex - 1 );
	for(i = 0; i < numVertex+1 ;i++){
		f[i] = -1;
	}
	for(i =0; i< numVertex+1; i++){
		for(j = i+1; j < numVertex; j++){
			if((values[i*numVertex + j] == 1)&&(values[j*numVertex + j] == 1)){
				f[i] = j;
			}
		}
	}
}

